import java.util.Scanner;

public class CinemaSystem {
    private static Scanner scanner = new Scanner(System.in);
    private static User currentUser = null;
    private static UserService userService;
    private static MovieService movieService;
    private static ScheduleService scheduleService;
    private static TicketService ticketService;

    public static void main(String[] args) {
        System.out.println("===== 欢迎使用影院管理系统 V1.5 =====");

        // 初始化服务类
        initializeServices();

        // 系统初始化
        initializeSystem();

        // 登录流程
        loginProcess();

        // 根据用户角色显示对应的功能菜单
        if (currentUser != null) {
            showMainMenu();
        }

        System.out.println("===== 感谢使用影院管理系统 =====");
        scanner.close();
    }

    // 初始化所有服务类
    private static void initializeServices() {
        userService = new UserService(scanner);
        movieService = new MovieService(scanner);
        scheduleService = new ScheduleService(scanner);
        ticketService = new TicketService(scanner);
    }

    // 系统初始化
    private static void initializeSystem() {
        System.out.println("系统初始化中...");
        // 检查数据库连接
        if (DBUtil.testConnection()) {
            System.out.println("数据库连接成功");
        } else {
            System.out.println("数据库连接失败，系统将退出");
            System.exit(0);
        }
        System.out.println("初始化完成！");
    }

    // 修改：将loginProcess()改为public static，允许外部调用
    public static void loginProcess() {
        int maxAttempts = 3;
        int attempts = 0;

        while (attempts < maxAttempts) {
            currentUser = userService.login();
            if (currentUser != null) {
                break;
            }
            attempts++;
            System.out.println("剩余尝试次数: " + (maxAttempts - attempts));
        }

        if (currentUser == null) {
            System.out.println("登录失败次数过多，系统退出！");
            System.exit(0);
        }
    }

    // 显示主菜单
    private static void showMainMenu() {
        boolean exit = false;

        while (!exit) {
            System.out.println("\n===== 主菜单 =====");
            System.out.println("1. 用户管理");
            System.out.println("2. 电影管理");
            System.out.println("3. 场次管理");
            System.out.println("4. 票务管理");
            System.out.println("5. 修改密码");
            System.out.println("6. 退出系统");
            System.out.print("请选择功能: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    if ("admin".equals(currentUser.getRole())) {
                        showUserManagementMenu();
                    } else {
                        System.out.println("权限不足，无法访问用户管理！");
                    }
                    break;
                case 2:
                    if ("admin".equals(currentUser.getRole()) || "manager".equals(currentUser.getRole())) {
                        showMovieManagementMenu();
                    } else {
                        System.out.println("权限不足，无法访问电影管理！");
                    }
                    break;
                case 3:
                    showScheduleManagementMenu();
                    break;
                case 4:
                    showTicketManagementMenu();
                    break;
                case 5:
                    changePasswordMenu();
                    break;
                case 6:
                    exit = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 用户管理菜单（仅管理员可见）
    private static void showUserManagementMenu() {
        boolean back = false;

        while (!back) {
            System.out.println("\n===== 用户管理 =====");
            System.out.println("1. 列出所有用户");
            System.out.println("2. 添加用户");
            System.out.println("3. 删除用户");
            System.out.println("4. 重置用户密码");
            System.out.println("5. 返回主菜单");
            System.out.print("请选择功能: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    userService.listAllUsers();
                    break;
                case 2:
                    userService.addUser();
                    break;
                case 3:
                    System.out.print("请输入要删除的用户名: ");
                    String delUsername = scanner.nextLine();
                    userService.deleteUser(delUsername);
                    break;
                case 4:
                    System.out.print("请输入要重置密码的用户名: ");
                    String resetUsername = scanner.nextLine();
                    System.out.print("请输入新密码: ");
                    String newPassword = scanner.nextLine();
                    userService.resetPassword(resetUsername, newPassword);
                    break;
                case 5:
                    back = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 电影管理菜单
    private static void showMovieManagementMenu() {
        boolean back = false;

        while (!back) {
            System.out.println("\n===== 电影管理 =====");
            System.out.println("1. 查看所有电影");
            System.out.println("2. 添加电影");
            System.out.println("3. 更新电影信息");
            System.out.println("4. 删除电影");
            System.out.println("5. 返回主菜单");
            System.out.print("请选择功能: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    movieService.listAllMovies();
                    break;
                case 2:
                    movieService.addMovie();
                    break;
                case 3:
                    movieService.updateMovie();
                    break;
                case 4:
                    movieService.deleteMovie();
                    break;
                case 5:
                    back = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 场次管理菜单
    private static void showScheduleManagementMenu() {
        boolean back = false;

        while (!back) {
            System.out.println("\n===== 场次管理 =====");
            System.out.println("1. 查看所有场次");
            System.out.println("2. 按电影ID查看场次");
            System.out.println("3. 添加场次");
            System.out.println("4. 更新场次信息");
            System.out.println("5. 删除场次");
            System.out.println("6. 返回主菜单");
            System.out.print("请选择功能: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    scheduleService.listAllSchedules();
                    break;
                case 2:
                    System.out.print("请输入电影ID: ");
                    String movieId = scanner.nextLine();
                    scheduleService.listSchedulesByMovieId(movieId);
                    break;
                case 3:
                    scheduleService.addSchedule();
                    break;
                case 4:
                    scheduleService.updateSchedule();
                    break;
                case 5:
                    scheduleService.deleteSchedule();
                    break;
                case 6:
                    back = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 票务管理菜单
    private static void showTicketManagementMenu() {
        boolean back = false;

        while (!back) {
            System.out.println("\n===== 票务管理 =====");
            System.out.println("1. 查看我的电影票");
            System.out.println("2. 购买电影票");
            System.out.println("3. 退票");

            // 管理员和经理可以查看所有票
            if ("admin".equals(currentUser.getRole()) || "manager".equals(currentUser.getRole())) {
                System.out.println("4. 查看所有电影票");
            }

            System.out.println((("admin".equals(currentUser.getRole()) || "manager".equals(currentUser.getRole())) ? "5" : "4") + ". 返回主菜单");
            System.out.print("请选择功能: ");

            int choice = Integer.parseInt(scanner.nextLine());

            switch (choice) {
                case 1:
                    ticketService.listUserTickets(currentUser.getUsername());
                    break;
                case 2:
                    ticketService.buyTicket(currentUser.getUsername());
                    break;
                case 3:
                    ticketService.refundTicket(currentUser.getUsername());
                    break;
                case 4:
                    if ("admin".equals(currentUser.getRole()) || "manager".equals(currentUser.getRole())) {
                        ticketService.listAllTickets();
                    } else {
                        back = true;
                    }
                    break;
                case 5:
                    if ("admin".equals(currentUser.getRole()) || "manager".equals(currentUser.getRole())) {
                        back = true;
                    } else {
                        System.out.println("无效的选择，请重新输入！");
                    }
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 修改密码菜单
    private static void changePasswordMenu() {
        System.out.print("请输入原密码: ");
        String oldPassword = scanner.nextLine();
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();
        System.out.print("请再次输入新密码: ");
        String confirmPassword = scanner.nextLine();

        if (!newPassword.equals(confirmPassword)) {
            System.out.println("两次输入的新密码不一致，修改失败！");
            return;
        }

        userService.changePassword(currentUser, oldPassword, newPassword);
    }

    // 提供获取当前用户的公共方法
    public static User getCurrentUser() {
        return currentUser;
    }
}
